/*
 *
   gcc Simple_Texture2D.c -L. -lGLESv2 -lmingw32 -lSDL2_test -lSDL2main -lSDL2 -lSDL2_ttf -lSDL2_image && a 
   gcc Simple_Texture2D.c SDL_test_common.c -lm -lSDL2 -lSDL2_ttf -lSDL2_image && ./a.out && ./a.out
   gcc Simple_Texture2D.c SDL_test_common.c -L. -lGLESv2 -lmingw32 -lSDL2main -lSDL2   && a
   adb push 2.c /sdcard/ &&
   Copyright (r) 1997-2014 Sam Lantinga <slouken@libsdl.org>

   This software is provided 'as-is', without any express or implied
   warranty.  In no event will the authors be held liable for any damages
   arising from the use of this software.

   Permission is granted to anyone to use this software for any purpose,
   including commercial applications, and to alter it and redistribute it
   freely.
   */
#include "gles2base.h"
#include "SDL2/SDL.h"

int Init ( UserData*userData)
{
	GLbyte vShaderStr[] =  
		"attribute vec4 a_position;   \n"
		"attribute vec2 a_texCoord;   \n"
		"varying vec2 v_texCoord;     \n"
		"void main()                  \n"
		"{                            \n"
		"   gl_Position = a_position; \n"
		"   v_texCoord = a_texCoord;  \n"
		"}                            \n";

	GLbyte fShaderStr[] =  
		"precision mediump float;                            \n"
		"varying vec2 v_texCoord;                            \n"
		"uniform sampler2D s_texture;                        \n"
		"void main()                                         \n"
		"{                                                   \n"
		"  gl_FragColor = texture2D( s_texture, v_texCoord );\n"
		"}                                                   \n";

	// Load the shaders and get a linked program object
	userData->programObject = esLoadProgram ( vShaderStr, fShaderStr );

	// Get the attribute locations
	userData->positionLoc = ctx.glGetAttribLocation ( userData->programObject, "a_position" );
	userData->texCoordLoc = ctx.glGetAttribLocation ( userData->programObject, "a_texCoord" );

	// Get the sampler location
	userData->samplerLoc = ctx.glGetUniformLocation ( userData->programObject, "s_texture" );

	// Load the texture
	SDL_Surface *surface = IMG_Load("0.jpg");
	SDL_FreeSurface(surface); surface = NULL;
	SDL_Color color = {0xff,0x00,0x00,0x00};
	surface = get_font(16,"0.jpg         ()",&color);
	if ( ! surface ) {
		SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to load icon.bmp: %s\n", SDL_GetError());
		userData->textureId = CreateSimpleTexture2D ();
	}else{
		GLfloat texcoords[4];
		userData->textureId = SDL_GL_LoadTexture(surface, texcoords);
		SDL_FreeSurface(surface);
	}

	ctx.glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f );
	return 1;
}

void Draw( UserData*userData)
{
	GLfloat vVertices[] = {
		-0.5f,  0.5f, 0.0f,  // Position 0
		0.0f,  0.0f,        // TexCoord 0 
		-0.5f, -0.5f, 0.0f,  // Position 1
		0.0f,  1.0f,        // TexCoord 1
		0.5f, -0.5f, 0.0f,  // Position 2
		1.0f,  1.0f,        // TexCoord 2
		0.5f,  0.5f, 0.0f,  // Position 3
		1.0f,  0.0f         // TexCoord 3
	};
	GLushort indices[] = { 0, 1, 2, 0, 2, 3 };
	// Set the viewport
	ctx.glViewport ( 0, 0, state->window_w, state->window_h);

	// Clear the color buffer
	ctx.glClear ( GL_COLOR_BUFFER_BIT );

	// Use the program object
	ctx.glUseProgram ( userData->programObject );

	// Load the vertex position
	ctx.glVertexAttribPointer ( userData->positionLoc, 3, GL_FLOAT, 
			GL_FALSE, 5 * sizeof(GLfloat), vVertices );
	// Load the texture coordinate
	ctx.glVertexAttribPointer ( userData->texCoordLoc, 2, GL_FLOAT,
			GL_FALSE, 5 * sizeof(GLfloat), &vVertices[3] );

	ctx.glEnableVertexAttribArray ( userData->positionLoc );
	ctx.glEnableVertexAttribArray ( userData->texCoordLoc );

	// Bind the texture
	ctx.glActiveTexture ( GL_TEXTURE0 );
	ctx.glBindTexture ( GL_TEXTURE_2D, userData->textureId );

	// Set the sampler texture unit to 0
	ctx.glUniform1i ( userData->samplerLoc, 0 );
	ctx.glDrawElements ( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices );
	/*eglSwapBuffers ( esContext->eglDisplay, esContext->eglSurface );*/
}

